package org.bonorsoft.modules.sysutils.login;

import org.apache.shiro.SecurityUtils;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.PasswordUtil;
import org.bonorsoft.modules.system.entity.SysUser;
import org.bonorsoft.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class LoginInfo {
    @Autowired
    private ISysUserService sysUserService;

    private static ISysUserService userService;

    @PostConstruct
    public void init() {
        userService = sysUserService;
    }

    public static LoginUser getLoginUser(){
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }

    public static boolean matches(String password) {
        LoginUser user = getLoginUser();
        SysUser sysUser = userService.getById(user.getId());
        String userpassword = PasswordUtil.encrypt(user.getUsername(), password, sysUser.getSalt());
        String syspassword = sysUser.getPassword();
        return syspassword.equals(userpassword);
    }
}
